package com.ccl.iot.device;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;

import com.ccl.iot.dvr.utils.LoggerUtil;
import com.ccl.iot.privates.BitmapCollector;
import com.ccl.iot.privates.BitmapMask;
import com.ccl.iot.privates.ForbidMask;
import com.ccl.iot.publics.UIUtils;
import com.ccl.iot.privates.WarningMask;
import com.ccl.iot.App;
import com.ccl.iot.IOTNetAPIEnum;

/**
 * @description：获取窗帘的图片
 * @author： HD
 * @time：2017/3/10
 */
public class DoorIconProvider extends DeviceIconProvider {

    public static boolean RegisterProviderType() {
        return RegisterProviderType(DoorIconProvider.class);
    }

    public synchronized static Bitmap Build(int style, int state, int width, int height, int highLightColor, int backColor, boolean isSelected, BitmapMask mask, int iconId) {
        if (width > 0 && height > 0) {
            Bitmap icon;
            String key = "Door." + style + "." + state + "=" + width + "x" + height + ":" + highLightColor + "-" + backColor + (mask == null ? "" : "+" + mask.getHashCode()) + "." + iconId;
            icon = BitmapCollector.Get(key);

            if (icon == null) {
                icon = Bitmap.createBitmap(width, height, backColor != Color.TRANSPARENT ? Config.RGB_565 : Config.ARGB_8888);

                Canvas canvas = new Canvas(icon);

                Bitmap centerIcon = null;

                //TODO 设置意向控制中门窗设备的图标
                if (iconId >= ICON_LIST_DOOR_STOP.length) {
                    iconId = 0;
                }

                if (state == IOTDoor.KDOOR_STATE_STOP) {
                    centerIcon = BitmapFactory.decodeResource(App.GetInstance().getResources(), ICON_LIST_DOOR_STOP[iconId]);
                } else if (state == IOTDoor.KDOOR_STATE_OPEN) {
                    centerIcon = BitmapFactory.decodeResource(App.GetInstance().getResources(), ICON_LIST_DOOR_OPEN[iconId]);
                } else if (state == IOTDoor.KDOOR_STATE_CLOSE) {
                    centerIcon = BitmapFactory.decodeResource(App.GetInstance().getResources(), ICON_LIST_DOOR_CLOSE[iconId]);
                }


                int size = width > height ? height : width;

                centerIcon = UIUtils.scaleBitmap(centerIcon, size, size);

                float left = (width - centerIcon.getWidth()) / 2;

                float top = (height - centerIcon.getHeight()) / 2;


                if (backColor != Color.TRANSPARENT) {
                    canvas.drawColor(backColor);
                }

                canvas.drawBitmap(centerIcon, left, top, new Paint());

                if (mask != null) {
                    mask.paintMask(canvas);
                }

                canvas.save();
                canvas.restore();

                BitmapCollector.Add(key, icon);

                return icon;
            }
            return icon;
        }
        return null;
    }

    public static int GetHighLigthColor(boolean aOnline, int aState, boolean aPressing) {
        int tColor = Color.DKGRAY;

        if (aOnline) {
            if (aState == IOTDoor.KDOOR_STATE_STOP) {
                tColor = (aPressing ? Color.rgb(200, 100, 100) : Color.RED);
            } else {
                if (aPressing) {
                    tColor = (aState == IOTDoor.KDOOR_STATE_OPEN ? Color.rgb(100, 200, 100) : Color.rgb(200, 200, 100));
                } else {
                    tColor = (aState == IOTDoor.KDOOR_STATE_OPEN ? Color.GREEN : Color.YELLOW);
                }
            }
        }

        return tColor;
    }

    public static int GetHighLigthColor(IOTDoor aDevice, boolean aPressing) {
        if (aDevice != null) {
            return GetHighLigthColor(aDevice.isOnline(), aDevice.getCurState(), aPressing);
        }

        return Color.TRANSPARENT;
    }

    public static Bitmap Build(int aStyle, int aWidth, int aHeight, int aBackColor, boolean aOnline, int aState, boolean aPressing, int aAuth, int iconId) {
        Bitmap tIcon = null;
        int tColor = GetHighLigthColor(aOnline, aState, aPressing);

        tIcon = Build(aStyle, aState, aWidth, aHeight, tColor, aBackColor, aPressing, null, iconId);

        return tIcon;
    }

    public static Bitmap Build(IOTDoor aDevice, int aStyle, int aWidth, int aHeight, int aBackColor, boolean aPressing) {
        if (aDevice != null) {
//            LoggerUtil.i("改变状态：" + aDevice.getCurState());
            return Build(aStyle, aWidth, aHeight, aBackColor, aDevice.isOnline(), aDevice.getCurState(), aPressing, aDevice.getAuth(), aDevice.getSplitIconId());
        }

        return null;
    }


    /*
     * 	Non-static functions
     */
    public DoorIconProvider(IOTDoor aDevice) {
        super(aDevice);
    }

    @Override
    public Bitmap build(int aStyle, int aWidth, int aHeight, int aBackColor, boolean aPressing) {
        return Build((IOTDoor) iDevice, aStyle, aWidth, aHeight, aBackColor, aPressing);
    }

}
